//
//  YXYFooterToolBar.swift
//  SinaWeibo
//
//  Created by 袁向阳 on 16/6/29.
//  Copyright © 2016年 YXY.cn. All rights reserved.
//

import UIKit

class YXYFooterToolBar: UIToolbar {
    
    // 供外界赋值
    var status: YXYStatus? {
        didSet {
            retweetBtn?.setTitle(status?.repostsStr, forState: UIControlState.Normal)
            commentBtn?.setTitle(status?.commentsStr, forState: UIControlState.Normal)
            zanBtn?.setTitle(status?.attitudesStr, forState: UIControlState.Normal)
        }
    }
    
    // 转发按钮
    var retweetBtn: UIButton?
    // 评论按钮
    var commentBtn: UIButton?
    // 点赞按钮
    var zanBtn: UIButton?
    // 线条01
    lazy var line1: UIButton = {
        let line = UIButton()
        line.setBackgroundImage(UIImage(named: "timeline_card_bottom_line"), forState: UIControlState.Normal)
        line.setBackgroundImage(UIImage(named: "timeline_card_bottom_line_highlighted"), forState: UIControlState.Normal)
        self.addSubview(line)
        return line
    }()
    // 线条02
    lazy var line2: UIButton = {
        let line = UIButton()
        line.setBackgroundImage(UIImage(named: "timeline_card_bottom_line"), forState: UIControlState.Normal)
        line.setBackgroundImage(UIImage(named: "timeline_card_bottom_line_highlighted"), forState: UIControlState.Normal)
        self.addSubview(line)
        return line
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
        backgroundColor = UIColor.whiteColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 添加子控件
    private func setUpUI() {
        retweetBtn = addChildSubview("timeline_card_bottom_background", imgName: "timeline_icon_retweet", title: "转发")
        commentBtn = addChildSubview("timeline_card_bottom_background", imgName: "timeline_icon_comment", title: "评论")
        zanBtn = addChildSubview("timeline_card_bottom_background", imgName: "timeline_icon_unlike", title: "赞")
        
        addSubViewConstraint()
    }
    
    // MARK: - 添加子控件约束
    private func addSubViewConstraint () {
        retweetBtn?.snp_makeConstraints(closure: { (make) -> Void in
            make.left.top.bottom.equalTo(self)
            make.width.equalTo(commentBtn!)
        })
        commentBtn?.snp_makeConstraints(closure: { (make) -> Void in
            make.top.bottom.equalTo(retweetBtn!)
            make.left.equalTo(retweetBtn!.snp_right)
            make.width.equalTo(zanBtn!)
        })
        zanBtn?.snp_makeConstraints(closure: { (make) -> Void in
            make.bottom.top.equalTo(commentBtn!)
            make.left.equalTo(commentBtn!.snp_right)
            make.right.equalTo(self)
        })
        
        line1.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self)
            make.centerX.equalTo(retweetBtn!.snp_right)
        }
        line2.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self)
            make.centerX.equalTo(commentBtn!.snp_right)
        }
    }
    
    // 给按钮设置title和图片
    func addChildSubview(backImg: String, imgName: String , title: String) -> UIButton {
        let button = UIButton()
        button.setTitle(title, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
//        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: backImg), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "\(backImg)_highlighted"), forState: .Highlighted)
        addSubview(button)
        return button
    }
}
